const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    https:"https://csb2c.fenxiangqu.cn/",
    cookie:"",
    ww:"100%",
    left:[],
    allcheck:1,
    _j:0,
    show:false,
    fullGood: [],/*满赠商品 */
    reduceGood: [],  /*满减商品*/
    comm:[], /*普通商品*/
    combination:[],
    addmoney:[],
    buying:[],
    shopcarComm:[],
    flag :true,
    flagf: false,
    __id:null,
    handiaoshow:false,
    oktiem:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    
  },
  gwcAddNum(e){
    
    var self = this;
    var id = e.currentTarget.dataset.textid,
        num = e.currentTarget.dataset.value,
        t = e.currentTarget.dataset.type;
        console.log(id)
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/" + (t == "tc" ? "xcx_Fun_package_wx" :"xcx_Fun_commodity_wx"),
      data: {
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        if(res.data == "0"){
          wx.showToast({
            title: '抱歉!请勾选商品',
            icon: 'success',
            duration: 1000
          })
        }else if(res.data == "1"){
          wx.showToast({
            title: '抱歉!该商品已下架',
            icon: 'success',
            duration: 1000
          })
        }else{
          wx.request({
            url: "https://csb2c.fenxiangqu.cn/" + (t == "tc" ? "xcx_combo_addGwc" : "xcx_50004_wx"),
            data:{
              xl:1,
              id:id
            },
            header:{
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': "LID=" + self.data.cookie
            },
            method:"POST",
            success:(res)=>{
              console.log(res)
              if (res.data.error == undefined){
                self.addgwc()
              }else{
                wx.showToast({
                  title: '商品异常，请重新加载',
                  icon: 'success',
                  duration: 1000
                })
              }
            },
            fail(res){
              wx.showToast({
                title: '网络异常，请重新加载',
                icon: 'success',
                duration: 1000
              })
            }
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /*组合套餐减商品 */
  FUN_reducePackage(id, num){
    var self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_Fun_package_wx",
      data: {
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        if (res.data == "0") {
          wx.showToast({
            title: '请勾选商品',
            icon: 'success',
            duration: 1000
          })
          return ;
        } else if (res.data == "1"){
          wx.showToast({
            title: '已下架',
            icon: 'success',
            duration: 1000
          })
          return;
        }else{
          +num--;
          if(num <=0){
            wx.showModal({
              title: '提示',
              content: '商品数量等于0，是否要将其进行删除？',
              success: function (res) {
                if (res.confirm) {
                  wx.request({
                    url: "https://csb2c.fenxiangqu.cn/xcx_combo_addGwc",
                    data: {
                      xl: -1,
                      id: id
                    },
                    header: {
                      'content-type': 'application/x-www-form-urlencoded',
                      'Cookie': "LID=" + self.data.cookie
                    },
                    method: "POST",
                    success: (res) => {
                      if (res.data.error == undefined) {
                        self.addgwc()
                      } else {
                        wx.showToast({
                          title: '商品异常，请重新加载',
                          icon: 'success',
                          duration: 1000
                        })
                      }
                    },
                    fail: (res) => {
                      wx.showToast({
                        title: '网络异常，请重新加载',
                        icon: 'success',
                        duration: 1000
                      })
                    }
                  })
                
                } else if (res.cancel) {
                  console.log('用户点击取消')
                  return false;
                }
              }
            })
          }else{
            wx.request({
              url: "https://csb2c.fenxiangqu.cn/xcx_combo_addGwc",
              data: {
                xl: -1,
                id: id
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded',
                'Cookie': "LID=" + self.data.cookie
              },
              method: "POST",
              success: (res) => {
                if (res.data.error == undefined) {
                  self.addgwc()
                } else {
                  wx.showToast({
                    title: '商品异常，请重新加载',
                    icon: 'success',
                    duration: 1000
                  })
                }
              },
              fail: (res) => {
                wx.showToast({
                  title: '网络异常，请重新加载',
                  icon: 'success',
                  duration: 1000
                })
              }
            })
          }
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  gwcreduceNum(e){
    var self = this;
    var id = e.currentTarget.dataset.textid,
        num = e.currentTarget.dataset.value,
        men = e.currentTarget.dataset.men,
        t = e.currentTarget.dataset.type,
        newMen = parseFloat(self.data._j) - parseFloat(men);
    console.log(newMen)
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_Fun_reduce_wx",
      data: {
        allPrice: newMen
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        if (res.data == "0" || res.data=="1" || res.data == "3") {
          if(t == 'tc'){
            self.FUN_reducePackage(id, num)
          }else{
            self.FUN_reduceCommodity(id, num)
          }
          
        } else {
          wx.showModal({
            title: '提示',
            content: '修改数量后，未满换购金额,不可选择换购商品',
            success: function (res) {
              if (res.confirm) {
                if (t == 'tc') {
                  self.FUN_reducePackage(id, num)
                } else {
                  self.FUN_reduceCommodity(id, num)
                }
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })

  },
  FUN_reduceCommodity(id,num){
    var self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_Fun_commodity_wx",
      data: {
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        if (res.data == "0") {
          wx.showToast({
            title: '抱歉!请勾选商品',
            icon: 'success',
            duration: 1000
          })
        } else if (res.data == "1") {
          wx.showToast({
            title: '抱歉!请勾选商品',
            icon: 'success',
            duration: 1000
          })
        } else {
          var n = parseFloat(num);
          n--;
          if(n<=0){
            wx.showModal({
              title: '温馨提示',
              content: '商品数量等于0，是否要将其进行删除？',
              success: function (res) {
                if (res.confirm) {
                  self.requestPren(id,function(e){
                    self.addgwc();
                  })
                } else if (res.cancel) {

                }
              }
            })
          }else{
            self.requestPren(id, function (e) {
              self.addgwc();
            })
          }
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  
  requestPren(id,callback){
    var self  = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_50004_wx",
      data: {
        xl:-1,
        id: id
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        if (res.data.error != undefined) {
          wx.showToast({
            title: '添加失败',
            icon: 'success',
            duration: 1000
          })
        } else {
          callback && callback(res)
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  chekcTap(e){
    var self = this;
    const id = e.currentTarget.dataset.id,
      men = +e.currentTarget.dataset.men,
      all = (parseFloat(self.data._j) - men).toFixed(2),
      t = e.currentTarget.dataset.t;
      let itype;
    if (e.currentTarget.dataset.type == 'tc'){
        itype = 1;
    }else{
        itype = 0;
    }
      wx.request({
        url: "https://csb2c.fenxiangqu.cn/xcx_Fun_reduce_wx",
        data: {
          allPrice: all
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': "LID=" + self.data.cookie
        },
        method: "POST",
        success: (res) => {
          console.log(res)
          if (res.data == "0" || res.data == "1" || res.data == "3") {
            self.commChk(id, itype, t);
          } else {
            wx.showModal({
              title: '提示',
              content: '修改数量后，未满换购金额,不可选择换购商品',
              success: function (res) {
                if (res.confirm) {
                  self.commChk(id, itype, t);
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          }
        },
        fail: (res) => {
          wx.showToast({
            title: '网络请求失败',
            icon: 'success',
            duration: 1000
          })
        }
      })    

  },
  commChk(id,itype,t){
      var self = this;
      var chk = 0;
      t ? chk = 0 : chk = 1;
      console.log(chk)
      wx.request({
        url: "https://csb2c.fenxiangqu.cn/xcx_50010_22_wx",
        data: {
          id: id,
          chk: chk,
          type:itype
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': "LID=" + self.data.cookie
        },
        method: "POST",
        success: (res) => {
          console.log(res)
          self.addgwc();
        },
        fail: (res) => {
          wx.showToast({
            title: '网络请求失败',
            icon: 'success',
            duration: 1000
          })
        }
      })
  
  },
  del:function(e){
    var self = this;
    const value = e.currentTarget.dataset.id;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_zb_000015x",
      data: {
        a: value,
        b:""
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        if(res.data == "1"){
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          })
          self.addgwc();
        }else{
          wx.showToast({
            title: '删除失败',
            icon: 'success',
            duration: 1000
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  jdel(res){
    const self =  this;
    const id = res.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: "https://csb2c.fenxiangqu.cn/xcx_combo_addGwc",
            data: {
              xl: -1,
              id: id
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cookie': "LID=" + self.data.cookie
            },
            method: "POST",
            success: (res) => {
              if (res.data.error == undefined) {
                self.addgwc()
              } else {
                wx.showToast({
                  title: '商品异常，请重新加载',
                  icon: 'success',
                  duration: 1000
                })
              }
            },
            fail: (res) => {
              wx.showToast({
                title: '网络异常，请重新加载',
                icon: 'success',
                duration: 1000
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
   
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    try {
      var res = wx.getSystemInfoSync();
      this.setData({
        ww: res.windowWidth + 'px'
      })

    } catch (e) {
      // Do something when catch error
      console.log(e)
    }

    wx.setNavigationBarTitle({
      title: '购物车'
    })
    var a = wx.getStorageSync('cookie') || [];
    this.setData({
      cookie: a,
      fullGood: [],/*满赠商品 */
      reduceGood: [],  /*满减商品*/
      comm: [] 
    })
    this.addgwc();
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  movediv:function(e){
    const num = e.currentTarget.dataset.num,
      c = e.currentTarget.dataset.c;
      if(c === "zh"){
        let html = 'combination[' + num + '].arr.changedTouches',
           html2 = 'combination[' + num + '].arr.left';
        if (this.data.combination[num].arr.changedTouches > e.changedTouches[0].pageX) {
          if (this.data.combination[num].arr.changedTouches - e.changedTouches[0].pageX>80){
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "-180rpx"
            })
          }
        }
        if (this.data.combination[num].arr.changedTouches < e.changedTouches[0].pageX) {
          if (e.changedTouches[0].pageX - this.data.combination[num].arr.changedTouches>80){
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "0px"
            })
          }
        }

      }else if (c === 'mj'){
        const nums =  e.currentTarget.dataset.nums
        let html = 'reduceGood[' + nums + '].arr[' + num +'].changedTouches',
          html2 = 'reduceGood[' + nums + '].arr[' + num +'].left';
          
        if (this.data.reduceGood[nums].arr[num].changedTouches > e.changedTouches[0].pageX) {
          if (this.data.reduceGood[nums].arr[num].changedTouches - e.changedTouches[0].pageX > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "-180rpx"
            })
          }
        }
        if (this.data.reduceGood[nums].arr[num].changedTouches < e.changedTouches[0].pageX) {
          if (e.changedTouches[0].pageX - this.data.reduceGood[nums].arr[num].changedTouches > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "0px"
            })
          }
        }
      }else if( c ==='mz'){
        const nums = e.currentTarget.dataset.nums
        let html = 'fullGood[' + nums + '].arr[' + num + '].changedTouches',
          html2 = 'fullGood[' + nums + '].arr[' + num + '].left';

        if (this.data.fullGood[nums].arr[num].changedTouches > e.changedTouches[0].pageX) {
          if (this.data.fullGood[nums].arr[num].changedTouches - e.changedTouches[0].pageX > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "-180rpx"
            })
          }
        }
        if (this.data.fullGood[nums].arr[num].changedTouches < e.changedTouches[0].pageX) {
          if (e.changedTouches[0].pageX - this.data.fullGood[nums].arr[num].changedTouches > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "0px"
            })
          }
        }
      }else if(c ==='pt'){
        let html = 'comm[0].arr[' + num + '].changedTouches',
          html2 = 'comm[0].arr[' + num + '].left';

        if (this.data.comm[0].arr[num].changedTouches > e.changedTouches[0].pageX) {
          if (this.data.comm[0].arr[num].changedTouches - e.changedTouches[0].pageX > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "-180rpx"
            })
          }
        }
        if (this.data.comm[0].arr[num].changedTouches < e.changedTouches[0].pageX) {
          if (e.changedTouches[0].pageX - this.data.comm[0].arr[num].changedTouches > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "0px"
            })
          }
        }
      }else if(c === 'xs'){
        let html = 'buying.arr[' + num + '].changedTouches',
          html2 = 'buying.arr[' + num + '].left';

        if (this.data.buying.arr[num].changedTouches > e.changedTouches[0].pageX) {
          if (this.data.buying.arr[num].changedTouches - e.changedTouches[0].pageX > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "-180rpx"
            })
          }
        }
        if (this.data.buying.arr[num].changedTouches < e.changedTouches[0].pageX) {
          if (e.changedTouches[0].pageX - this.data.buying.arr[num].changedTouches > 80) {
            this.setData({
              [html]: e.changedTouches[0].pageX,
              [html2]: "0px"
            })
          }
        }
      }
    
  },
  addgwc:function(){
    const self = this;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_AllGuc23",
      data: {
        a: ""
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + this.data.cookie
      },
      method: "POST",
      success: (res) => {
        console.log(res)
        // self.data.show = true;        
          if(res.data =="login"){
            // self.data.show = false;
            wx.showModal({
              title: '登录提示',
              content: '您未登录，是否前往登录？',
              success: function (res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '../center/center'
                  })
                } else if (res.cancel) {
                  wx.switchTab({
                    url: '../home/home'
                  })
                }
              }
            })
          }
          let full = [] , reduce = [], comm=[],buying={},addm= [];
          let _json = res.data;
          let allcheck = 1;
          let foot = "";
          let _j = 0;
          let tc_j = 0;
          let jqg_j = 0;
          let cxjg = 0;
          let json_length = res.data[0].length;//商品
          let json_tcLength = res.data[2].length;//组合套餐
          let jqg_Length = res.data[3].length;//加钱购活动
          let jqgList_Length = res.data[4].length;//加钱购商品
          let spcxhtml = "";
          let isNllcx = 0;
          
          if (json_tcLength > 0){
            for (let j = 0; j < _json[2].length; j++){
              _json[2][j].arr = {
                "left": "0px",
                "changedTouches": 0
              }
              const tc_price = (_json[2][j].packageprice).toFixed(2);
              _json[2][j].newp = (+tc_price * (_json[2][j].packageNum | 0)).toFixed(2)
              if (_json[2][j].chk == 1){
                tc_j = +tc_j + (+tc_price * (_json[2][j].packageNum | 0))
              }else{
                allcheck = 0;
              }
            }
            _j = _j + tc_j
          }


          if (_json[0].length > 0){
            _json[1].forEach( (item, index)=>{
              
              let kt = { comm: [],arr:[]}
               if (item[ 0 ] == '123456789'){
                 if (item[ 5 ] == '无促销'){

                 }else{
                   if (item[ 5 ] == '满赠') {
                     
                   } else {
                     item[2] = +((item[2]).toFixed(2))
                     item[3] = +((item[3]).toFixed(2))
                   }
                 }
               }else {
                  kt.rule = item;
                 cxjg += +((item[2]).toFixed(2))
               }
               _json[0].forEach((item2, index2)=>{
                 if (!item2[ 0 ].type){
                   //满赠满减商品
                    if (item2[ 1 ].length > 0){
                       for(let i=0; i<item2[1].length; i++){
                         if (item2[1][i][2] == true){
                           if (item[0] ==  item2[1][i][0]._id){
                             var _coll;
                              if (item[3] == '1'){
                                  _coll = true;
                              }else{
                                  _coll = false;
                              }
                              let sp_price = (item2[4]).toFixed(2);
                              let _sp_price = (+sp_price * (1 - item[8])).toFixed(2);
                              item2[0]._sp_price = _sp_price;
                              item2[0].pr = (+sp_price * (item2[0].num | 0)).toFixed(2)
                              item2[0]._coll = _coll;
                              item2[0].json = JSON.stringify(item2[1])
                              kt.arr.push({
                                "left": "0px",
                                "changedTouches": 0
                              })

                              kt.comm.push(item2)
                              if(item2[2] == true){
                                _j = +_j + (+sp_price * item2[0].num)
                              } else{
                                allcheck = 0;
                              }
                           }
                         }
                       }

                    }

                    //普通商品
                    if (item[ 0 ] == '123456789'){
                      if (item2[ 1 ].length == 0){
                         let _coll = false;
                         let sp_price = (item2[4]).toFixed(2);
                         let _sp_price = (+sp_price * (1 - item[8])).toFixed(2);
                         if (item2[ 3 ] == '1'){
                           _coll = true;
                         } else {
                           _coll = false;
                         }
                         item2[0]._sp_price = _sp_price;
                         item2[0].pr = (+sp_price * (item2[0].num | 0)).toFixed(2)
                         item2[0]._coll = _coll;
                         kt.comm.push(item2)
                         kt.arr.push({
                           "left": "0px",
                           "changedTouches": 0
                         })
                         if (item2[2] == true) {
                           _j = +_j + (+sp_price * item2[0].num)
                         } else {
                           allcheck = 0;
                         }
                         isNllcx++;
                      }
                    }
                 }
               })
               if (item[ 5 ] == '满赠') {
                 kt.zp = []; kt.zp2 = [];
                 for(let i =0; i<item[ 6 ].length; i++){
                   kt.zp.push(item[6][i])
                 }
                 for(let i=0; i<item[ 7 ].length; i++){
                   kt.zp2.push(item[ 7 ][i])
                 }
               }
               if (item[0] == '123456789') {
                 if (item[5] == '无促销') {
                   comm.push(kt)
                 } else {
                   comm.push(kt)
                 }
               } else {
                 if (item[5] == '满赠') {
                   full.push(kt)
                 }else{
                    reduce.push(kt)
                 }
               }
            });
            {
              let sate = false;
              buying = {comm:[],arr:[]}
              for(let i= 0; i<_json[0].length; i++){
                if(_json[0][i][0].type){
                    if(sate){
                      sate = false
                    }
                    let _coll;
                    if (_json[0][i][3] == "1") {

                      _coll = true;
                    } else {
                      _coll = false;
                    }
                    console.log(_json[0][i][0])
                    let sp_price = parseFloat(_json[0][i][0].spxsj).toFixed(2)
                    let _sp_price = (parseFloat(_json[0][i][0].spxsj) * parseInt(_json[0][i][0].num))
                    _json[0][i][0].price = sp_price;
                    _json[0][i][0].pr = (parseFloat(_json[0][i][0].spxsj) * parseInt(_json[0][i][0].num)).toFixed(2)
                    _json[0][i][0]._coll = _coll;
                    buying.comm.push(_json[0][i])
                    buying.arr.push({
                      "left": "0px",
                      "changedTouches": 0
                    })
                    if (_json[0][i][2] == true) {
                      _j = parseFloat(_j) + (parseFloat(_json[0][i][0].spxsj) * parseInt(_json[0][i][0].num));
                    } else {
                      allcheck = 0;
                    }
                }
              }
            }
          }                   
            
          if (+((_j).toFixed(2)) - cxjg > (_json[3].satisfy)){
            if (jqgList_Length > 0){
              for(let i=0; i<_json[4].length; i++){
                var jqg_price = (_json[4][i].exchangePrice).toFixed(2);
                _json[4][i].exchangePrice = jqg_price;
                _json[4][i].pr = (parseFloat(jqg_price) * parseInt(_json[4][i].userQuota)).toFixed(2)
                addm.push({
                  comm: _json[4][i], arr: {
                    "left": "0px",
                    "changedTouches": 0
                  }})
                if (_json[4][i].chk == 1) {
                  jqg_j = parseFloat(jqg_j) + (parseFloat(jqg_price) * parseInt(_json[4][i].userQuota));
                } else {
                  allcheck = 0;
                }
              }
              _j = parseFloat(_j) + parseFloat(jqg_j);
            }
          }
          self.setData({
            __id: _json[3]._id,
            comm: comm,
            fullGood: full,
            addmoney: addm,
            shopcarComm: _json,
            reduceGood: reduce,
            combination: _json[2],
            buying:buying,
            allcheck: allcheck,
            _j: (+(_j.toFixed(2)) - cxjg).toFixed(2)
          })
          wx.hideToast()
          console.log(self)
        
      },
      fail: (res) => {
        wx.showToast({
          title: '购物车加载失败',
          icon: 'success',
          duration: 1000
        })
        wx.hideToast()
      }
    })
    
  },
  /*跳转加钱购 */
  mj(e){
    const id = e.currentTarget.dataset.id,
      money = e.currentTarget.dataset.money;
      wx.navigateTo({
        url: `../hg/hg?id=${id}&money=${money}`
      })
  },
  allChecks:function(e){
    var self = this;
    const num = e.currentTarget.dataset.c;
    if(num == "1"){
      var chk = 0;
    }else{
      var chk = 1;
    }
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_50010_22_wx",
      data: {
        id: "123",
        chk: chk
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        self.addgwc();
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  mongNum:function(e){
    const self = this;
    let delnum = 0;
    let tc_delnum = 0;
    let jqg_delnum = 0;
    let spidlist = "";
    const fullGood = this.data.fullGood, reduceGood = this.data.reduceGood, comm = this.data.comm,
      combination = this.data.combination, addmoney = this.data.addmoney;
    if (combination.length > 0) {
      for (let i = 0; i < combination.length; i++) {
        if (combination[i].chk == 1) {
          if (tc_delnum > 0) {
            spidlist += ',';
          }
          tc_delnum += 1;
          delnum += tc_delnum;
          spidlist += combination[i].packageId;
        }
      }
    }
    
    if (addmoney.length > 0) {
      for (let i = 0; i < addmoney.length; i++) {
        if (addmoney[i].comm.chk == 1) {
          if (tc_delnum > 0) {
            spidlist += ',';
          }
          jqg_delnum += 1;
          delnum += jqg_delnum;
          spidlist += addmoney[i].comm._id
        }
      }
    }
    
    if (fullGood.length > 0){
      console.log(fullGood)
      for (let i = 0; i < fullGood.length; i++){
        for (let j = 0; j < fullGood[i].comm.length;j++){

          if (fullGood[i].comm[j][2] == true){
            if (delnum > 0){
              spidlist +=','
            }
            delnum += 1;
            spidlist += fullGood[i].comm[j][0].goodsId;
          }
        }
      }
    }
    
    if (reduceGood.length > 0) {
      for (let i = 0; i < reduceGood.length; i++) {
        for (let j = 0; j < reduceGood[i].comm.length; j++) {
          if (reduceGood[i].comm[j][2] == true) {
            if (delnum > 0) {
              spidlist += ','
            }
            delnum += 1;
            spidlist += reduceGood[i].comm[j][0].goodsId;
          }
        }
      }
    }
    if (comm.length>0){
      if (comm[0].comm.length > 0) {

        for (let i = 0; i < comm[0].comm.length; i++) {
          if (comm[0].comm[i][2] == true) {
            if (delnum > 0) {
              spidlist += ','
            }
            delnum += 1;
            spidlist += comm[0].comm[i][0].goodsId;
          }
        }
      }
    }   
    

    if(delnum == 0){
      wx.showToast({
        title: '请勾选',
        icon: 'success',
        duration: 1500
      })
    }else{
      wx.request({
        url: "https://csb2c.fenxiangqu.cn/xcx_zb_000031x",
        data: {
          a: ""
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': "LID=" + self.data.cookie
        },
        method: "POST",
        success: (res) => {
          if (res.data.a != "" || res.data.b != "") {
            var mess = "抱歉";
            if (res.data.a != "") {
              mess += res.data.a + "已下架";
            }
            if (res.data.b != "") {
              mess += res.data.b + "库存不足";
            }
            wx.showToast({
              title: mess,
              icon: 'success',
              duration: 2000
            })
          } else {
            console.log(spidlist)
            app.globalData['yyyyy'] = ""
            wx.navigateTo({
              url: '../submit/submit?spidlist=' + spidlist + '&type=showcart' + '&gmtype=tjdd'
            })
          }
        },
        fail: (res) => {
          wx.showToast({
            title: '网络请求失败',
            icon: 'success',
            duration: 1000
          })
        }
      })
    }
        
      
    
  },
  jqgchk(res){
      const self= this;
      const e = res.currentTarget.dataset;
      let chk = 0;
      const jqgid = e.dd, hg_id = e.id ,t = e.t;
      if(t){
        chk = 0;
      }else{
        chk = 1;
      }
      wx.request({
        url: "https://csb2c.fenxiangqu.cn/xcx_50010_22_wx",
        data: {
          id: hg_id,
          chk: chk,
          hg_id: jqgid,
          type:2
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'Cookie': "LID=" + self.data.cookie
        },
        method: "POST",
        success: (res) => {
          self.addgwc()
        },
        fail: (res) => {
          wx.showToast({
            title: '网络请求失败',
            icon: 'success',
            duration: 1000
          })
        }
      })
  },
  /*修改商品促销规则 */
  updateItem(e){
    const data = e.currentTarget.dataset;
    const json = JSON.parse(data.json),
    spid=data.spid, cxid = data.cxid;
    let arr = [];
    for(let i =0; i< json.length;i++){
      json[i][0].pid = spid;
      if(cxid =="" || cxid == null){
          if(i == 0){
            json[i][0].c = true;
            arr.push(json[i])
          }else{
            arr.push(json[i])
          }
          
      }else{
        if (cxid == json[i][0]._id){
          
          json[i][0].c = true;
          arr.push(json[i])
        }else{
          arr.push(json[i])
        }
      }
    }

    this.setData({
      handiaoshow:true,
      oktiem: arr
    })



  },
  okt(e){
    const self = this;
    const data = e.currentTarget.dataset;
    const spid = data.spid, gzid = data.gzid, name = data.name;
    wx.request({
      url: "https://csb2c.fenxiangqu.cn/xcx_50010_21_wx",
      data: {
        id: spid,
        hdid: gzid,
        name: name
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "LID=" + self.data.cookie
      },
      method: "POST",
      success: (res) => {
        self.addgwc();
        self.setData({
          handiaoshow:false
        })
      },
      fail: (res) => {
        wx.showToast({
          title: '网络请求失败',
          icon: 'success',
          duration: 1000
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      handiaoshow:false
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})